package com.coco.service.impl;

import com.coco.constant.MongoTableNameConst;
import com.coco.model.*;
import com.coco.proxy.ManagerFeign;
import com.coco.service.FacadeService;
import com.coco.utils.CoPageResp;
import com.coco.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author GHOSTLaycoo
 * @date 2020/4/16 0016 - 上午 10:44
 */
@Service
public class FacadeServiceImpl implements FacadeService{

    @Autowired
    private ManagerFeign managerFeign;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<NaviInfo> getNaviInfoList(NaviInfoCondition naviInfoCondition,String userId) {
        ResponseEntity<List<NaviInfo>> naviByCondition = managerFeign.getNaviByCondition(naviInfoCondition);
        ResponseEntity<List<VideoModel>> listResponseEntity = managerFeign.selectCollectionByUserId(userId);
        //统计数据
        if(listResponseEntity!=null){
            List<VideoModel> body = listResponseEntity.getBody();
            if(!CollectionUtils.isEmpty(body)){
                Map<String, Long> collect = body.stream().collect(Collectors.groupingBy(VideoModel::getTypeId,Collectors.counting()));
                for (NaviInfo naviInfo : naviByCondition.getBody()) {
                    if(collect.get(naviInfo.getId())!=null){
                        naviInfo.setNum(collect.get(naviInfo.getId()));
                    }
                }
                naviByCondition.getBody().sort(Comparator.comparing(NaviInfo::getNum).reversed());
            }
        }
        if(CollectionUtils.isEmpty(naviByCondition.getBody())&&naviByCondition.getBody().size()<0){
            return null;
        }
        return naviByCondition.getBody();
    }

    @Override
    public List<VideoModel> getVideoForOne(VideoCondition videoCondition) {
        Criteria typeId = Criteria.where("typeId").in(videoCondition.getQuery().getTypeId());
        org.springframework.data.mongodb.core.query.Query query1 = new org.springframework.data.mongodb.core.query.Query(typeId);
        org.springframework.data.mongodb.core.query.Query query = query1.with(Sort.by(Sort.Order.desc("sum"))).skip((videoCondition.getPageIndex()-1)*videoCondition.getPageSize()).limit(videoCondition.getPageSize());
        List<VideoModel> ts = mongoTemplate.find(query,VideoModel.class,MongoTableNameConst.CoCo_Video);
        long count = mongoTemplate.count(query1, MongoTableNameConst.CoCo_Video);
//        ResponseEntity<CoPageResp<VideoModel>> videosByCondition = managerFeign.getVideosByCondition(videoCondition);
//        List<VideoModel> data = videosByCondition.getBody().getData();
//        if(CollectionUtils.isEmpty(data)||data.size()<0){
//            return null;
//        }
        return ts;
    }

    @Override
    public List<VideoModel> getVideoByToday() {
        VideoCondition videoCondition = new VideoCondition();
        videoCondition.setPageIndex(1);
        videoCondition.setPageSize(6);
        Query query = new Query();
        Date startTime = CommonUtils.initDateByDay();
        Date endTime = CommonUtils.getNeedTime(23, 59, 59, 0);
        Date[] dates = new Date[2];
        dates[0] = startTime;
        dates[1] = endTime;
        query.setTimes(dates);
        videoCondition.setQuery(query);
        ResponseEntity<CoPageResp<VideoModel>> videosByCondition = managerFeign.getVideosByCondition(videoCondition);
        if(videosByCondition.getBody()==null){
            return null;
        }
        List<VideoModel> data = videosByCondition.getBody().getData();
        if(CollectionUtils.isEmpty(data)||data.size()<0){
            return null;
        }
        return data;
    }

    @Override
    public VideoModel getVideoById(String id) {
        ResponseEntity<VideoModel> videoById = managerFeign.getVideoById(id);
        if(videoById.getBody()==null){
            return null;
        }
        return videoById.getBody();
    }

    @Override
    public CoPageResp<VideoModel> getVideoByCondition(VideoCondition videoCondition) {
        ResponseEntity<CoPageResp<VideoModel>> videosByCondition = managerFeign.getVideosByCondition(videoCondition);
        if(videosByCondition.getBody()==null){
            return null;
        }
        return videosByCondition.getBody();
    }
}
